
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:sjx/route/route_utils.dart';
import 'package:sjx/route/routes.dart';
import 'package:sjx/utils/sp_utils.dart';

import '../../constants.dart';

class SettingPage extends StatefulWidget {
  const SettingPage({super.key});

  @override
  State<StatefulWidget> createState() {
    return _SettingPageState();
  }
}

class _SettingPageState extends State<SettingPage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('设置'),
        leading: IconButton(
          icon: const Icon(Icons.arrow_back),
          onPressed: () => Navigator.of(context).pop(),
        ),
      ),
      backgroundColor: Color.fromRGBO(244, 247, 250, 1),
      body: Stack(
        children: [
          SingleChildScrollView(
            padding: const EdgeInsets.only(bottom: 100),
            child: Column(
              children: [
                _buildSettingItem('个人资料', onTap: () {
                  RouteUtils.pushForNamed(context, RoutePath.person_info);
                }),
                const Divider(height: 1, indent: 15),
                _buildSettingItem('提现账户', onTap: () {
                  RouteUtils.pushForNamed(context, RoutePath.band_withdraw_account);
                }),
                // const Divider(height: 1, indent: 15),
                // _buildSettingItem('安全设置', onTap: () {
                //   // Navigate to SettingSecurityPage
                //   // Navigator.push(context, MaterialPageRoute(builder: (context) => SettingSecurityPage()));
                // }),
                const SizedBox(height: 12),
                _buildSettingItem('关于软件', onTap: () {
                  RouteUtils.pushForNamed(context, RoutePath.about_our);
                }),
                const SizedBox(height: 12),
                // _buildSettingItem('任务设置', onTap: () {
                //   // Navigate to TaskSettingPage
                //   // Navigator.push(context, MaterialPageRoute(builder: (context) => TaskSettingPage()));
                // }),
                // const Divider(height: 1, indent: 15),
                _buildSettingItem('注销账户', onTap: () {
                  RouteUtils.pushForNamed(context, RoutePath.logoff);
                }),
              ],
            ),
          ),
          Positioned(
            bottom: 20,
            left: 15,
            right: 15,
            child: SizedBox(
              width: double.infinity,
              height: 44,
              child: ElevatedButton(
                style: ElevatedButton.styleFrom(
                  backgroundColor: Theme.of(context).colorScheme.primary,
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(22),
                  ),
                  elevation: 0,
                ),
                child: Container(
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(22),
                  ),
                  child: const Center(
                    child: Text(
                      '退出登录',
                      style: TextStyle(
                        fontSize: 16,
                        fontWeight: FontWeight.w500,
                        color: Colors.white,
                      ),
                    ),
                  ),
                ),
                onPressed: _showLogoutDialog,
              ),
            ),
          ),
        ],
      ),

    );
  }

  Widget _buildSettingItem(String title, {VoidCallback? onTap}) {
    return Material(
      color: Colors.white, // 背景色
      child: InkWell(
        onTap: onTap,
        child: Container(
          padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 15),
          child: Row(
            children: [
              Expanded(
                child: Text(
                  title,
                  style: const TextStyle(fontSize: 16, color: Color(0xFF262626)),
                ),
              ),
              const Icon(Icons.arrow_forward_ios, size: 16),
            ],
          ),
        ),
      ),
    );
  }

  void _showLogoutDialog() {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return AlertDialog(
          title: const Text('提示'),
          content: const Text('是否要退出当前账号？'),
          actions: [
            TextButton(
              child: const Text('取消'),
              onPressed: () => Navigator.of(context).pop(),
            ),
            TextButton(
              child: const Text('退出'),
              onPressed: () {
                Navigator.of(context).pop();
                _performLogout();
              },
            ),
          ],
        );
      },
    );
  }

  void _performLogout() {
    SpUtils.clearUserData();
    RouteUtils.pushNamedAndRemoveUntil(context, RoutePath.tab);
  }
}